﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.IO;

namespace temp01
{
    public partial class _Default : System.Web.UI.Page
    {
        private static string getIP()
        {
            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                //return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] { ',' })[0];
                return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        }

        private static void writeFile(string Value)
        {
            string strFilePath = "C:\\new.txt";
            FileStream fs = new FileStream(strFilePath, FileMode.Append, FileAccess.Write);
            StreamWriter fs2 = new StreamWriter(fs);
            fs2.WriteLine(DateTime.Now.ToString()+ " " +Value);
            fs2.Flush();
            fs2.Close();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = Label1.Text = DateTime.Now.ToString() + getIP();
            writeFile(getIP());            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Label1.Text = DateTime.Now.ToString() + getIP();
            writeFile(getIP());
        }
    }
}